﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraNavBar;

namespace WorkflowDesigner
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FlowDesinger : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public FlowDesinger()
        {
            InitializeComponent();
            InitToolBox();

            FlowNodes = new List<AuditNode>();
            Lines = new List<Line>();
        }

        public Panel WorkSpace
        {
            get
            {
                return this.workSpace;
            }
        }

        public IList<AuditNode> FlowNodes
        {
            get;
            private set;
        }

        public IList<Line> Lines
        {
            get;
            private set;
        }

        private void AddNewProcessNode()
        {
            AuditNode node = new AuditNode(this);
            this.workSpace.Controls.Add(node);

            Point p = this.workSpace.PointToClient(Cursor.Position);
            p = new Point(p.X - node.Width / 2, p.Y - node.Height / 2);
            if (p.X < 0) p.X = 0;
            if (p.Y < 0) p.Y = 0;

            node.Location = p;
            node.BringToFront();
            FlowNodes.Add(node);
            this.SelectFlowNode(node);
        }

        private void SelectFlowNode(AuditNode node)
        {
            foreach (var item in FlowNodes)
            {
                if (item.UniqueId == node.UniqueId)
                    item.IsSelected = true;
                else
                    item.IsSelected = false;
            }
        }

        private NavBarItemLink GetItemLink(IDataObject data)
        {
            NavBarItemLink ret = data.GetData(typeof(NavBarItemLink)) as NavBarItemLink;
            return ret;
        }

        private void workSpace_DragDrop(object sender, DragEventArgs e)
        {
            NavBarItemLink link = GetItemLink(e.Data);
            if (link != null && link.Item.Enabled)
            {
                if (link.Item.Tag != null && link.Item.Tag.ToString() == typeof(AuditNode).ToString())
                {
                    AddNewProcessNode();
                }
                if (link.Item.Tag != null && link.Item.Tag.ToString() == typeof(Line).ToString())
                {
                    AddNewLine();
                }
            }
        }

        private void AddNewLine()
        {
            if (this.FlowNodes.Count > 1)
            {
                var line = new Line();
                line.StartNode = this.FlowNodes[0];
                line.EndNode = this.FlowNodes[1];
                Lines.Add(line);
                line.Draw(this.workSpace.CreateGraphics());
            }
        }

        private void workSpace_DragEnter(object sender, DragEventArgs e)
        {
            NavBarItemLink link = GetItemLink(e.Data);
            if (link != null && link.Item.Enabled)
                e.Effect = DragDropEffects.Copy;
            else e.Effect = DragDropEffects.None;
        }
        /// <summary>
        /// 初始化工具箱
        /// </summary>
        private void InitToolBox()
        {
            DevExpress.XtraNavBar.NavBarItem nbiProcessNode = new NavBarItem();
            nbiProcessNode.Caption = "审批结点";
            nbiProcessNode.Name = "nbiProcessNode";
            nbiProcessNode.Tag = typeof(AuditNode);

            DevExpress.XtraNavBar.NavBarItem nbiProcessLine = new NavBarItem();
            nbiProcessLine.Caption = "连线";
            nbiProcessLine.Name = "nbiProcessLine";
            nbiProcessLine.Tag = typeof(Line);

            this.NavBar.Items.AddRange(new DevExpress.XtraNavBar.NavBarItem[] {
            nbiProcessNode,nbiProcessLine});
            nbgFlow.ItemLinks.AddRange(new DevExpress.XtraNavBar.NavBarItemLink[] {
            new DevExpress.XtraNavBar.NavBarItemLink(nbiProcessNode),
             new DevExpress.XtraNavBar.NavBarItemLink(nbiProcessLine)
            });
        }

        private void workSpace_Paint(object sender, PaintEventArgs e)
        {
            foreach (var item in Lines)
            {
                item.Draw(e.Graphics);
            }
        }
    }
}
